package com.bdqn.controller;

import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * IndexController
 *
 * @author LILIBO
 * @since 2022-02-20
 */
@Controller
public class IndexController {

    @Resource
    private UserService userService;

    @GetMapping("/login")
    public String login() {
        System.out.println("login");
        return "login";
    }

    @PostMapping("/login")
    public String login(String usrName, String usrPassword, Model model, HttpSession session) {
        System.out.println("login " + usrName + " " + usrPassword);
        User user = userService.loginUser(usrName, usrPassword);
        if (user != null) {
            session.setAttribute("loginUser", user);
            return "main";
        } else {
            model.addAttribute("msg", "用户名或密码错误，登录失败！");
            return "login";
        }
    }
}
